<?php
class ReportController extends BackendApplicationController
{
	public $layout = 'application.modules.bill.views.layouts.global';
	public $time=null;
	
	public function init() {
		parent::init();
	
		if (isset($_GET['datetime']) && $_GET['datetime'] != "") {
			$createdTime = $_GET['datetime'];
			if (strrpos($createdTime, "-")) {
				$createdTime = explode("-", $createdTime);
				$fromDate = explode("/", trim($createdTime[0]));
				$fromDate = $fromDate[2] . "-" . str_pad($fromDate[0], 2, '0', STR_PAD_LEFT) . "-" . str_pad($fromDate[1], 2, '0', STR_PAD_LEFT);
				$toDate = explode("/", trim($createdTime[1]));
				$toDate = $toDate[2] . "-" . str_pad($toDate[0], 2, '0', STR_PAD_LEFT) . "-" . str_pad($toDate[1], 2, '0', STR_PAD_LEFT);
				$this->time = array('from' => $fromDate, 'to' => $toDate);
			} else {
				$time = explode("/", trim($_GET['datetime']));
				$time = $time[2] . "-" . str_pad($time[0], 2, '0', STR_PAD_LEFT) . "-" . str_pad($time[1], 2, '0', STR_PAD_LEFT);
				$this->time = array('from' => $time, 'to' => $time);
			}
		}
		/* if(!empty($this->time)){
		echo '<pre>';print_r($this->time);die();
		} */
	}
	
	public function actionIndex()
	{
		$data = null;
		if(!empty($this->time)){
			$data = BackendBillModel::getByDate($this->time);
			//echo '<pre>';print_r($data);die();
			if(isset($_GET['export']) && $_GET['export']=='export'){
				$this->layout=false;
				header('Content-Type: text/html; charset=utf-8');
				$title = "bao_cao_doanh_thu";
				header('Content-type: application/vnd.ms-excel');
				header("Content-Disposition: attachment; filename=$title.xls");
				header("Pragma: no-cache");
				header("Expires: 0");
				$this->renderPartial('_export_excel', array(
						'data'=>$data
				), false);
				Yii::app()->end();
			}
		}
		$this->render('index', array(
				'data'=>$data
		));
	}
}